<!DOCTYPE html>
<html class="layui-bg-white">
<head th:include="include :: header"></head>
<body>
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form">
            <input type="hidden" id="paperId" name="paperId" th:value="${paperId}"/>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">题号</label>
                    <div class="layui-input-block">
                        <input type="text" name="sortNumber" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">题目</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" placeholder="请输入" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">科目</label>
                    <div class="layui-input-block">
                        <select id="subjectId" name="subjectId" class="form-control chosen-select" lay-search>
                            <option value="">--请选择--</option>
                            <option th:each="item : ${subject}" th:value="${item.id}" th:text="${item.name}"></option>
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-operate" lay-submit lay-filter="LAY-search">搜索</button>
                    <button class="layui-btn layuiadmin-btn-operate" id="addQuestion">添加</button>
                </div>
            </div>
        </div>
        <div class="layui-tab layui-tab-card" lay-filter="layui-tab-card">
            <ul class="layui-tab-title">
                <li class="layui-this">选择题目</li>
                <li>综合题目</li>
            </ul>
            <div class="layui-tab-content">
                <div class="layui-tab-item layui-show">
                    <table id="LAY-table0" lay-filter="LAY-table1"></table>
                </div>

                <div class="layui-tab-item layui-show">
                    <table id="LAY-table1" lay-filter="LAY-table1"></table>
                </div>
            </div>
        </div>
    </div>
</div>
<div th:include="include :: footer"></div>
<script type="text/javascript">
    layui.config({
        base: ctx + '/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index'         //主入口模块
    }).use(['index', 'form', 'table'], function () {
        var tabIndex = 0; //当前选项卡的index
        var $ = layui.$, form = layui.form, table = layui.table, element = layui.element;
        var paperId = $('#paperId').val() || '';

        //选项卡切换事件
        element.on('tab(layui-tab-card)', function (obj) {
            if (obj.index == 0) {
                commonJs.tableRender(table, cols, ctx + '/manage/choice/choiceQuestion/list', 'full-155', null, 'LAY-table0');
            } else {
                commonJs.tableRender(table, cols, ctx + '/manage/synthesis/synthesisQuestion/list', 'full-155', null, 'LAY-table1');
            }
            tabIndex = obj.index;
        });

        //添加题目
        $('#addQuestion').click(function () {
            var checkStatus;
            if (tabIndex == 0) {
                var checkStatus = table.checkStatus('LAY-table0')
            } else if (tabIndex == 1) {
                var checkStatus = table.checkStatus('LAY-table1')
            }
            var checkData = checkStatus.data; //得到选中的数据
            if (checkData.length === 0) {
                return commonJs.openMsg('warning', '请选择要添加的题目！');
            }
            var ids = new Array();
            $.each(checkData, function (i, row) {
                ids[i] = row['id'];
            });
            var data = {'paperId': paperId, 'ids': ids.join(','), type: (tabIndex + 1)};
            commonJs.openConfirm('确定添加题目吗？', function () {
                commonJs.openAjax(ctx + "/manage/paper/paperQuestion/save", data, function (result) {
                    if (result.code == 0) {
                        commonJs.openMsg('success', result.msg, function () {
                            parent.paperQuestion.reload();
                            var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                            parent.layer.close(index);
                        });
                    } else {
                        commonJs.openMsg('error', result.msg);
                    }
                });
            });
        });

        //监听搜索
        form.on('submit(LAY-search)', function (data) {
            var field = data.field;
            //执行重载
            if (tabIndex == 0) {
                table.reload('LAY-table0', {
                    where: field
                });
            } else if (tabIndex == 1) {
                table.reload('LAY-table1', {
                    where: field
                });
            }
        });

        var cols = [{
            type: "checkbox",
            fixed: "left"
        }, {
            field: "id",
            title: "ID",
            width: 80
        }, {
            field: "subjectName",
            title: "科目",
            width: 200
        }, {
            field: "sortNumber",
            title: "题号",
            width: 80
        }, {
            field: "type",
            title: "类型",
            width: 80,
            templet: function (d) {
                if (tabIndex == 0) {
                    if (d.type == '1') {
                        return '单选';
                    } else if (d.type == '2') {
                        return '多选';
                    }
                } else if (tabIndex == 1) {
                    if (d.type == '1') {
                        return '问答';
                    } else if (d.type == '2') {
                        return '论文';
                    }
                }
            }
        }, {
            field: "name",
            title: "题目"
        }];

        //初始化table
        commonJs.tableRender(table, cols, ctx + '/manage/choice/choiceQuestion/list', 'full-155', null, 'LAY-table0');
    })
</script>
</body>
</html>